<?php
	function addMenuType($name, $parent=0, $level=0, $position=1)
	{
		$insert  = 'INSERT INTO '.DB_PERFIX.'com_menu (`id`, `name`, `parent_id`, `level`, `position`)'; 
		$insert .= 'VALUES (NULL, "'.$name.'", '.$parent.', '.$level.', '.$position.');';
		$result = mysql_query($insert);
		if ($result > 0)
		{
			$result = mysql_query("SELECT id FROM ".DB_PERFIX."com_menu WHERE name LIKE '".$_POST['name']."';");
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['title'] = $name;
			}
			updateMenuTypeLevels($item['id']);
		}
		return $item;
	}
	
	function getMenuTopLevel($id)
	{
		$toplevel = 0;
		$result = mysql_query('SELECT level FROM '.DB_PERFIX.'com_menu WHERE parent_id='.$id.' ORDER BY level DESC LIMIT 1;');
		while($row = mysql_fetch_array($result))
		{ $toplevel = $row['level']; }
		return $toplevel;
	}
	
	function hasMenuPosition($id, $level, $position)
	{
		$out = false;
		$result = mysql_query('SELECT id FROM '.DB_PERFIX.'com_menu WHERE parent_id='.$id.' AND level='.$level.' AND position='.$position.' LIMIT 1;');
		while($row = mysql_fetch_array($result))
		{ $out = true; }
		return $out;
	}
	
	function addMenuLevel($id)
	{
		if($id > 0)
		{
			$next = getMenuTopLevel($id);
			$next++;
			
			$feedback = addMenuType('Level '.$next, $id, $next);
			return $feedback;
		}
	}
	
	function addMenuFirst($id, $level)
	{
		if($id > 0)
		{
			$existing = 0;
			$select ='SELECT id FROM '.DB_PERFIX.'com_menu WHERE parent_id='.$id.' AND level='.$level.' AND position=0;';
			$result = mysql_query($select);
			
			while($row = mysql_fetch_array($result))
			{ $existing = $row['id']; }
			if($existing == 0)
			{ $feedback = addMenuType('Level '.$level.' First', $id, $level, 0); }
			updateMenuTypeLevels($id);
			return $feedback;
		}
	}
	
	function addMenuLast($id, $level)
	{
		if($id > 0)
		{
			$existing = 0;
			$result = mysql_query('SELECT id FROM '.DB_PERFIX.'com_menu WHERE parent_id='.$id.' AND level='.$level.' AND position=2;');
			while($row = mysql_fetch_array($result))
			{ $existing = $row['id']; }
			if($existing == 0)
			{ $feedback = addMenuType('Level '.$level.' Last', $id, $level, 2); }
			updateMenuTypeLevels($id);
			return $feedback;
		}
	}
	
	function getMenuTypeAdmin($id, $level=0, $position=1)
	{
		$select = 'SELECT * FROM '.DB_PERFIX.'com_menu WHERE level='.$level.' AND position='.$position;
		
		if(($level > 0) || ($position != 1))
		{ $select .= ' AND parent_id='.$id.';'; }
		else
		{ $select .= ' AND id='.$id.';';}
		$result = mysql_query($select);
		while($row = mysql_fetch_array($result))
		{
			$menu_type['id'] = $row['id'];
			$menu_type['parent_id'] = $row['parent_id'];
			$menu_type['type'] = $row['type'];
			$menu_type['levels'] = $row['levels'];
			$menu_type['level'] = $row['level'];
			$menu_type['position'] = $row['position'];
			$menu_type['name'] = $row['name'];
			$menu_type['l_image'] = $row['l_image'];
			$menu_type['l_image_h'] = $row['l_image_h'];
			$menu_type['l_image_a'] = $row['l_image_a'];
			$menu_type['bg_image'] = $row['bg_image'];
			$menu_type['bg_image_h'] = $row['bg_image_h'];
			$menu_type['bg_image_a'] = $row['bg_image_a'];
			$menu_type['r_image'] = $row['r_image'];
			$menu_type['r_image_h'] = $row['r_image_h'];
			$menu_type['r_image_a'] = $row['r_image_a'];
			$menu_type['active'] = $row['active'];
			if($menu_type['parent_id'] > 0)
			{ 
				$menu_type['css'] = getCssArray(CSS_TYPE_ID, $menu_type['parent_id']);
				$menu_type['id'] = $menu_type['parent_id'];
			}
			else
			{ $menu_type['css'] = getCssArray(CSS_TYPE_ID, $menu_type['id']); }
		}
		return $menu_type;
	}
	
	function getMenuTypes($incat=0, $parent=0, $level=0, $type='')
	{
		$i=0;
		if(($type != '') && (($type == 0) || ($type > 0)))
		{ $typeadd = ' AND type='.$type; }
		
		if($incat > 0)
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'com_menu WHERE parent_id='.$parent.' AND level='.$level.' AND id IN(SELECT item_id FROM '.DB_PERFIX.'admin_hfb_cats_to_items WHERE type='.CSS_TYPE_ID.' AND cat_id='.$incat.');'); }
		else
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'com_menu WHERE parent_id='.$parent.' AND level='.$level.$typeadd.';'); }
		while($row = mysql_fetch_array($result))
		{
			$menu_type[$i]['id'] = $row['id'];
			$menu_type[$i]['parent_id'] = $row['parent_id'];
			$menu_type[$i]['type'] = $row['type'];
			$menu_type[$i]['levels'] = $row['levels'];
			$menu_type[$i]['level'] = $row['level'];
			$menu_type[$i]['position'] = $row['position'];
			$menu_type[$i]['name'] = $row['name'];
			$menu_type[$i]['l_image'] = $row['l_image'];
			$menu_type[$i]['l_image_h'] = $row['l_image_h'];
			$menu_type[$i]['l_image_a'] = $row['l_image_a'];
			$menu_type[$i]['bg_image'] = $row['bg_image'];
			$menu_type[$i]['bg_image_h'] = $row['bg_image_h'];
			$menu_type[$i]['bg_image_a'] = $row['bg_image_a'];
			$menu_type[$i]['r_image'] = $row['r_image'];
			$menu_type[$i]['r_image_h'] = $row['r_image_h'];
			$menu_type[$i]['r_image_a'] = $row['r_image_a'];
			$menu_type[$i]['active'] = $row['active'];
			$i++;
		}
		return $menu_type;
	}
	
	function updateMenuType($id, $name, $type)
	{
		if($id > 0)
		{
			$name = stripNonAlphaNum($name); 
			$update  = 'UPDATE '.DB_PERFIX.'com_menu SET ';
			$update .= 'name="'.$name.'", ';
			$update .= 'type='.$type.' ';
			$update .= 'WHERE id='.$id.' LIMIT 1;';
			$update = mysql_query($update);
			
			$update  = 'UPDATE '.DB_PERFIX.'com_menu SET ';
			$update .= 'type='.$type.' ';
			$update .= 'WHERE parent_id='.$id.';';
			$update = mysql_query($update);
			
			updateMenuTypeLevels($id);
		}
	}
	
	function updateMenuTypeLevels($id)
	{
		if($id > 0)
		{
			$levels = getMenuTopLevel($id);
			$levels++;
			
			$update  = 'UPDATE '.DB_PERFIX.'com_menu SET ';
			$update .= 'levels='.$levels.' ';
			$update .= 'WHERE id='.$id.' OR parent_id='.$id.';';
			$update = mysql_query($update);
		}
	}
	
	function deleteMenuLevel($id, $level)
	{
		if(($id > 0) && ($level > 0))
		{
			$items = getMenuTypes(0, $id, $level);
			if($items)
			foreach($items as $item)
			{
				if($item['l_image'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image', $item['l_image']); }
				if($item['l_image_h'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image_h', $item['l_image_h']); }
				if($item['l_image_a'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image_a', $item['l_image_a']); }
				if($item['r_image'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image', $item['r_image']); }
				if($item['r_image_h'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image_h', $item['r_image_h']); }
				if($item['r_image_a'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image_a', $item['r_image_a']); }
				if($item['bg_image'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image', $item['bg_image']); }
				if($item['bg_image_h'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image_h', $item['bg_image_h']); }
				if($item['bg_image_a'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image_a', $item['bg_image_a']); }
			}
			$result = mysql_query('DELETE FROM '.DB_PERFIX.'com_menu WHERE parent_id='.$id.' AND level='.$level.';');
		}
		updateMenuTypeLevels($id);
	}
	
	function deleteMenuType($id)
	{
		if($id > 0)
		{
			$item = getMenuTypeAdmin($id);
			if($item)
			{
				if($item['l_image'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image', $item['l_image']); }
				if($item['l_image_h'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image_h', $item['l_image_h']); }
				if($item['l_image_a'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image_a', $item['l_image_a']); }
				if($item['r_image'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image', $item['r_image']); }
				if($item['r_image_h'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image_h', $item['r_image_h']); }
				if($item['r_image_a'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image_a', $item['r_image_a']); }
				if($item['bg_image'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image', $item['bg_image']); }
				if($item['bg_image_h'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image_h', $item['bg_image_h']); }
				if($item['bg_image_a'] != '')
				{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image_a', $item['bg_image_a']); }
			
				$items = getMenuTypes(0, $id);
				if($items)
				foreach($items as $item)
				{
					if($item['l_image'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image', $item['l_image']); }
					if($item['l_image_h'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image_h', $item['l_image_h']); }
					if($item['l_image_a'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'l_image_a', $item['l_image_a']); }
					if($item['r_image'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image', $item['r_image']); }
					if($item['r_image_h'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image_h', $item['r_image_h']); }
					if($item['r_image_a'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'r_image_a', $item['r_image_a']); }
					if($item['bg_image'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image', $item['bg_image']); }
					if($item['bg_image_h'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image_h', $item['bg_image_h']); }
					if($item['bg_image_a'] != '')
					{ deleteMenuTypeImage($item['id'], $item['level'], $item['position'], 'bg_image_a', $item['bg_image_a']); }
				}
				$result = mysql_query('DELETE FROM '.DB_PERFIX.'admin_css WHERE item_id='.$id.' AND type='.CSS_TYPE_ID.';');
				$result = mysql_query('DELETE FROM '.DB_PERFIX.'com_menu WHERE id='.$id.' OR parent_id='.$id.';');
				return $result;
			}
		}
	}
	
	function deleteMenuTypeImage($id, $level, $position ,$field, $filename)
	{
		$result = mysql_query('UPDATE '.DB_PERFIX.'com_menu SET '.$field.'="" WHERE position='.$position.' AND level='.$level.' AND (id='.$id.' OR parent_id='.$id.');');
		if ($result > 0)
		{
			if (!unlink(MENU_PATH.$filename))
			{ $status = "Error deleting ".MENU_PATH.$filename; }
			else
			{$error[$field] = "Image Deleted";}
		}
		return $error;
	}
	
	function uploadMenuTypeImages($id, $level=0, $position=1, $imagenames)
	{
		if(($imagenames) && ($id > 0))
		{
			foreach($imagenames as $imagename)
			{
				
				if (image_valid($_FILES[$imagename]['type']) && ($_FILES[$imagename]['size'] < 20000) && ($_FILES[$imagename]['name'] != ""))
				{
					$temp_name = $_FILES[$imagename]["name"];
					$extension = substr($temp_name,-3);
					$pos = '';
					if($position == 2)
					{ $pos = '_last'; }
					if($position == 0)
					{ $pos = '_first'; }
					
					$new_name = 'type_'.$id.'_'.$level.'_'.str_ireplace('_image','',$imagename).$pos.'.'.$extension;
					
					if (file_exists(MENU_PATH.$new_name))
					{$error[$imagename] = $_FILES[$imagename]["name"] . " already exists. ";}
					else
					{
						move_uploaded_file($_FILES[$imagename]["tmp_name"], MENU_PATH.$new_name);
						$error[$imagename] = "Stored in: " . MENU_PATH.$new_name;
						if(($position != 1) || ($level > 0))
						{ $where = ' parent_id='.$id.' AND level='.$level.' AND position='.$position.';'; }
						else
						{ $where = ' id='.$id.';'; }
						$result = mysql_query('UPDATE '.DB_PERFIX.'com_menu SET '.$imagename.'="'.$new_name.'" WHERE '.$where.';');
						if ($result > 0)
						{$error[$imagename] = "<strong>Image Uploaded ".$new_name."</strong>";}
					}
					$extension = "";
				}
			}
		}
	}
?>